//
//  XJLLoginViewController.m
//  XJL_WeChat
//
//  Created by XJL on 16/7/30.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLLoginViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "XJLChangeAccountViewController.h"
#import "XJLRegisterViewController.h"

@interface XJLLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *loginAccountLable;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation XJLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.pwdTextField becomeFirstResponder];
    //监听文字长度的改变
    //使用ReactiveCocoa(RAC) 监听文字的改变,(思想是信号,信号被订阅)
    @weakify(self)
    [self.pwdTextField.rac_textSignal subscribeNext:^(NSString *text) {
        @strongify(self)
        self.loginBtn.enabled = text.length > 0;
    }];
    
    //监听按钮的点击事件（按钮信号）
    [[self.loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        [self doLoging];
        
    }];
    //显示上次登录的账号
    NSString *lastAccount = [NSUserDefaults getLastLoginAccount];
    if (lastAccount) {
        self.loginAccountLable.text = lastAccount;
    }
    
}

//执行登录请求
-(void)doLoging{
    [SVProgressHUD showWithStatus:@"登录中..."];
    
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.loginAccountLable.text password:self.pwdTextField.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {
//            XJLLog(@"登录成功");
            // 设置自动登录
            [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            
            //进入主界面
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            self.view.window.rootViewController = sb.instantiateInitialViewController;
        }else{
            //登录失败
            [SVProgressHUD showErrorWithStatus:error.description];
        }
    } onQueue:nil];
    
}

-(void)setLoginAccount:(NSString *)loginAccount{
    _loginAccount = loginAccount;
    self.loginAccountLable.text = loginAccount;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // 点空白区域,键盘消失
    [self.view endEditing:YES];
}

- (IBAction)moreAction {
    //由下往上弹窗
    UIAlertController *moreAlert = [UIAlertController alertControllerWithTitle:@"moreAction" message:@"moreAction2" preferredStyle:UIAlertControllerStyleActionSheet];
    // 1.添加"切换帐户"的按钮
    UIAlertAction *changeAccount = [UIAlertAction actionWithTitle:@"切换账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        XJLChangeAccountViewController *changeVc = [[XJLChangeAccountViewController alloc]init];
        [self presentViewController:changeVc animated:YES completion:nil];
    }];
    [moreAlert addAction:changeAccount];
    // 2.添加"注册"的按钮
    UIAlertAction *registerAction = [UIAlertAction actionWithTitle:@"注册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        XJLRegisterViewController *registerVc = [[XJLRegisterViewController alloc]init];
        [self presentViewController:registerVc animated:YES completion:nil];
    }];
    [moreAlert addAction:registerAction];
     // 3.添加"帮助"的按钮
    UIAlertAction *helpAction = [UIAlertAction actionWithTitle:@"help" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [moreAlert addAction:helpAction];
    // 4.添加"取消"的按钮
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [moreAlert addAction:cancleAction];
    
    [self presentViewController:moreAlert animated:YES completion:nil];
}


@end
